#include "Map.hpp"

namespace sl
{
	namespace tmp
	{
		MapS::MapS()
		{
		}

		MapS::MapS(const std::string& file)
		{
			Load(file);
		}

		MapS* MapS::Load(const std::string& file)
		{
			try
			{
				if (!maploader::Map::Load(file))
				{
					::sl::Exception("", ::sl::ErrorCode::LoadMap);
					throw std::exception(std::string("can't load the map " + file).c_str());
				}

				//dynamic_cast<maploader::Map&>(*this).Load(file);
				mySpriteTilesets.clear();
				for (std::vector<maploader::Tileset>::iterator it = myTilesets.begin(); it != myTilesets.end(); it++)
					mySpriteTilesets.push_back(Tileset(*it));

				myFirstGID.clear();
				for (std::vector<Tileset>::iterator it = mySpriteTilesets.begin(); it != mySpriteTilesets.end(); it++)
					myFirstGID.push_back(it->GetFirstGID());

			} catch (std::exception& e) {
				Exception(e.what(), ErrorCode::LoadMap);
			}

			return this;
		}

		MapS* MapS::Draw(unsigned int index)
		{
			sf::Vector2f pos;
			//for (unsigned int layer = 0; layer < myLayer.size(); layer++)
			//{
			if (index >= 0 && index < myLayer.size())
			{
				unsigned int layer = index;
				for (unsigned int y = 0; y < myHeight; y++)
					for (unsigned int x = 0; x < myWidth; x++)
					{
						unsigned int index = myLayer[layer].GetData()[y][x];
						if (index != 0)
						{
							//std::cout << x << "x" << y << ":" << index << " " << GetLocalID(index) << " " << GetTilesetIndex(index) << " " << myTileWidth << " " << myTileHeight << std::endl;
							pos = sf::Vector2f((x * myTileWidth) + (y % 2) * myTileWidth / 2, (y * myTileHeight) / 2);
							sf::RenderTarget* tar = RenderTextureManager->Get("map");
							if (tar == NULL)
								tar = (*RenderScreen)();

							if (tar->GetDefaultView().GetViewport().Intersects(tar->GetDefaultView().GetViewport(), sf::FloatRect(pos.x, pos.y, myTileWidth, myTileHeight)))
								mySpriteTilesets[GetTilesetIndex(index)].DrawTile(GetLocalID(index), pos, myTileHeight, RenderTextureManager->Get("layer" + Convert(layer)));
						}
					}
			} else {
				// Fehler ausgeben
			}

			return this;
		}

		MapS* MapS::Draw(const std::string& layer)
		{
			for (unsigned int index = 0; index < myLayer.size(); index++)
				if (myLayer[index].GetName() == layer)
					Draw(index);

			return this;
		}

		MapS* MapS::Draw()
		{
			for (unsigned int index = 0; index < myLayer.size(); index++)
				Draw(index);

			return this;
		}

		unsigned int MapS::GetLocalID(unsigned int globalID)
		{
			unsigned int index = globalID;
			for (std::vector<unsigned int>::iterator it = myFirstGID.begin(); it != myFirstGID.end(); it++)
				if (*it <= index)
					index -= *it;

			return index;
		}

		unsigned int MapS::GetTilesetIndex(unsigned int globalID)
		{
			unsigned int index = 0;
			for (std::vector<unsigned int>::iterator it = myFirstGID.begin(); it != myFirstGID.end(); it++)
				if (*it <= globalID)
					index++;

			return index - 1;
		}
	}
}